//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "EverestOption.h"
using namespace Cephei::QL::Experimental;
#include <gen/QL/Exercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/MultiAssetOption.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::CEverestOption::CEverestOption (Double notional, Double guarantee, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer) : CMultiAssetOption(CEverestOption::typeid)
{
    CExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phEverestOption = NULL;
#endif
        QuantLib::Real _notional = (QuantLib::Real)ValueHelper::Convert (notional); //d
        QuantLib::Rate _guarantee = (QuantLib::Rate)ValueHelper::Convert (guarantee); //d
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        _ppEverestOption = new boost::shared_ptr<QuantLib::EverestOption> (new QuantLib::EverestOption ( _notional,  _guarantee,  _exercise ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppEverestOption)->setPricingEngine (_QL_Pricer);
        SetMultiAssetOption (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppEverestOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cexercise != nullptr) _Cexercise->Unlock();
    }
}
Cephei::QL::Experimental::CEverestOption::CEverestOption (boost::shared_ptr<QuantLib::EverestOption>& childNative, Object^ owner) : CMultiAssetOption(CEverestOption::typeid)
{
#ifdef HANDLE
	_phEverestOption = NULL;
#endif
	_ppEverestOption = &childNative;
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppEverestOption));
}
Cephei::QL::Experimental::CEverestOption::CEverestOption (QuantLib::EverestOption& childNative, Object^ owner) : CMultiAssetOption(CEverestOption::typeid)
{
#ifdef HANDLE
	_phEverestOption = NULL;
#endif
	_ppEverestOption = new boost::shared_ptr<QuantLib::EverestOption> (&childNative);
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppEverestOption));
    _EverestOptionOwner = owner;
    _MultiAssetOptionOwner = owner;
}

Cephei::QL::Experimental::CEverestOption::CEverestOption (CEverestOption^ copy) : CMultiAssetOption(CEverestOption::typeid)
{
#ifdef HANDLE
	_phEverestOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEverestOption = new boost::shared_ptr<QuantLib::EverestOption> (copy->GetShared());
        _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppEverestOption));
    }
}
Cephei::QL::Experimental::CEverestOption::CEverestOption (PLATFORM::Type^ t) : CMultiAssetOption(CEverestOption::typeid)
{
#ifdef HANDLE
	_phEverestOption = NULL;
#endif
	if (!t->IsSubclassOf(CEverestOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::CEverestOption::CEverestOption (QuantLib::Handle<QuantLib::EverestOption>& childNative, Object^ owner)  : CMultiAssetOption(CEverestOption::typeid)
{
	_phEverestOption = &childNative;
	_ppEverestOption = &static_cast<boost::shared_ptr<QuantLib::EverestOption>>(childNative.currentLink());
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppEverestOption));
    _EverestOptionOwner = owner;
}
Cephei::QL::Experimental::CEverestOption::CEverestOption (QuantLib::Handle<QuantLib::EverestOption> childNative)  : CMultiAssetOption(CEverestOption::typeid)
{
	_phEverestOption = &childNative;
	_ppEverestOption = &static_cast<boost::shared_ptr<QuantLib::EverestOption>>(childNative.currentLink());
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppEverestOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::CEverestOption::CEverestOption (QuantLib::EverestOption childNative)  : CMultiAssetOption(CEverestOption::typeid)
{
#ifdef HANDLE
	_phEverestOption = NULL;
#endif
	_ppEverestOption = new boost::shared_ptr<QuantLib::EverestOption> (new QuantLib::EverestOption (childNative));
    _ppMultiAssetOption = new boost::shared_ptr<QuantLib::MultiAssetOption> (boost::dynamic_pointer_cast<QuantLib::MultiAssetOption> (*_ppEverestOption));
}
#endif

Cephei::QL::Experimental::CEverestOption::~CEverestOption ()
{
    if (_ppEverestOption != NULL)
    {
	    delete _ppEverestOption;
        _ppEverestOption = NULL;
    }
}
Cephei::QL::Experimental::CEverestOption::!CEverestOption ()
{
    if (_ppEverestOption != NULL)
    {
	    delete _ppEverestOption;
    }
}
QuantLib::EverestOption& Cephei::QL::Experimental::CEverestOption::GetReference ()
{
    if (_ppEverestOption == NULL) throw REFNEW NativeNullException ();
	return **_ppEverestOption;
}
boost::shared_ptr<QuantLib::EverestOption>& Cephei::QL::Experimental::CEverestOption::GetShared ()
{
    if (_ppEverestOption == NULL) throw REFNEW NativeNullException ();
	return *_ppEverestOption;
}
QuantLib::EverestOption* Cephei::QL::Experimental::CEverestOption::GetPointer ()
{
    if (_ppEverestOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppEverestOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EverestOption>& Cephei::QL::Experimental::CEverestOption::GetHandle ()
{
	if (_phEverestOption == NULL)
	{
		_phEverestOption = new Handle<QuantLib::EverestOption> (*_ppEverestOption);
	}
	return *_phEverestOption;
}
#endif
bool Cephei::QL::Experimental::CEverestOption::HasNative () 
{
	return (_ppEverestOption != NULL);
}

Double Cephei::QL::Experimental::CEverestOption::Yield::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppEverestOption)->yield ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::IEverestOption^ Cephei::QL::Experimental::CEverestOption_Factory::Create (Double notional, Double guarantee, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CEverestOption ( notional,  guarantee,  exercise,  QL_Pricer);
}
